package models

import (
	_ "github.com/jinzhu/gorm"
)

type Product struct {
	Id              int //与Menu.relation、productCate.Id关联
	Title           string
	SubTitle        string
	ProductSn       string
	CateId          int
	ClickCount      int
	ProductNumber   int
	Price           float64
	MarketPrice     float64
	RelationProduct string
	ProductAttr     string
	ProductVersion  string
	ProductImg      string
	ProductGift     string
	ProductFitting  string
	ProductColor    string
	ProductKeywords string
	ProductDesc     string
	ProductContent  string
	IsDelete        int
	IsHot           int
	IsBest          int
	IsNew           int
	ProductTypeId   int
	Sort            int
	Status          int
	AddTime         int
}

func (Product) TableName() string {
	return "product"
}

//通过传入的dateId从productcate中找出符合类别的产品，通过产品的id与productType在product列表中进一步检索
//productCate将产品id和产品所属类关联起来
func GetProductByCategory(cateId int, productType string, limitNum int) []Product {
	product := []Product{}
	productCate := []ProductCate{}
	DB.Where("pid=?", cateId).Find(&productCate)
	var tempSlice []int
	if len(productCate) > 0 {
		for i := 0; i < len(productCate); i++ {
			tempSlice = append(tempSlice, productCate[i].Id)
		}
	}
	tempSlice = append(tempSlice, cateId)
	where := "cate_id(?)"
	switch productType {
	case "hot":
		where += "AND is_hot=1"
	case "best":
		where += "AND is_best=1"
	case "new":
		where += "AND is_new=1"
	default:
		break
	}
	DB.Where(where, tempSlice).Select("id,title,price,product_img,sub_title").Limit(limitNum).Order("sort desc").Find(&product)
	return product
}
